home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 58606 / 58606.xpi / chrome / translator.jar / content / page.js < prev    next >
Text (UTF-16)  |  2010-02-07  |  13KB  |  251 lines

  1.  
  2. (function(namespace, $)
  3. {
  4.     namespace.Page = function(translator, properties)
  5.     {
  6.         this.translator = translator;
  7.         this.properties = properties;
  8.     };
  9.     
  10.     namespace.Page.prototype = {
  11.         translator: null,
  12.         properties: null,
  13.         id: null,
  14.         window: null,
  15.         doc: null,
  16.         popup: null,
  17.         floatingPanel: null,
  18.         
  19.         init: function(doc)
  20.         {
  21.             this.doc = doc;
  22.             this.window = doc.defaultView;
  23.             this.popup = new namespace.Popup(this.doc);
  24.             this.floatingPanel = new namespace.FloatingPanel(this.doc);
  25.             
  26.             // generate page id
  27.             this.id = ((new Date()).getTime() + '' + Math.floor(Math.random() * 1000000)).substr(0, 18);
  28.             
  29.             // add event listeners
  30.             this.addListeners();
  31.             
  32.             // add page unload listener
  33.             this.window.addEventListener('unload', this.pageUnloadHandler.bind(this), false);
  34.             
  35.             // add ui listeners if translator is already enabled
  36.             if(this.translator.isEnabled()) {
  37.                 this.addTranslatorListeners();
  38.             }
  39.         },
  40.         
  41.         addListeners: function()
  42.         {
  43.             $(document).bind('translatorEnabled.translator.' + this.id, this.translatorEnabledHandler.bind(this));
  44.             $(document).bind('translatorDisabled.translator.' + this.id, this.translatorDisabledHandler.bind(this));
  45.             $(document).bind('translatorTranslateSelectionEnabled.translator.' + this.id, this.translateSelectionEnabledHandler.bind(this));
  46.             $(document).bind('translatorTranslateSelectionDisabled.translator.' + this.id, this.translateSelectionDisabledHandler.bind(this));
  47.             $(document).bind('translatorTranslateFloating.translator.' + this.id, this.translateFloatingHandler.bind(this));
  48.         },
  49.         
  50.         removeListeners: function()
  51.         {
  52.             $(document).unbind('.' + this.id);
  53.         },
  54.         
  55.         addTranslatorListeners: function()
  56.         {
  57.             if(this.translator.isTranslationBySelectionEnabled() || this.translator.isTranslationByFloatingEnabled()) {
  58.                 this.addMouseListeners();
  59.             }
  60.             
  61.             // set translation event listener on page doc
  62.             $(this.doc).bind('translatorTranslateSelection.translator', this.translateSelectionHandler.bind(this));
  63.             
  64.             // mousedown listener always needed if tranlator is enabled for closing popup
  65.             $(this.doc).unbind('mousedown.translator').bind('mousedown.translator', function(e) {
  66.                 if(this.popup && this.popup.el && this.popup.visible) {
  67.                     // if popup itself
  68.                     if($(e.target).closest('.translator-popup').length > 0) return false;
  69.                     
  70.                     // hide popup
  71.                     this.popup.hide();
  72.                 }
  73.                 if(this.floatingPanel && this.floatingPanel.visible) {
  74.                     // if panel itself
  75.                     if($(e.target).closest('.translator-floating-panel').length > 0) return false;
  76.                     
  77.                     // hide panel
  78.                     this.floatingPanel.hide();
  79.                 }
  80.             }.bind(this));
  81.         },
  82.         
  83.         removeTranslatorListeners: function()
  84.         {
  85.             $(this.doc).unbind('.translator');
  86.         },
  87.         
  88.         addMouseListeners: function()
  89.         {
  90.             $(this.doc).unbind('mouseup.translator').bind('mouseup.translator', function(e) {
  91.                 if(e.which !== 1) return true;
  92.                 if(!this.translator.isEnabled()) return true;
  93.                 
  94.                 // if translation popup itself
  95.                 if(this.popup && this.popup.el && $(e.target).closest('.translator-popup').length > 0) return true;
  96.                 
  97.                 // if no text selected
  98.                 if(!this.isTextSelected()) return;
  99.                 
  100.                 if(this.translator.isTranslationBySelectionEnabled()) {
  101.                     // if no popup
  102.                     if(!this.popup) return true;
  103.                     
  104.                     this.popup.setPosition(e.clientX, e.clientY);
  105.                     this.translateSelection();
  106.                 }
  107.                 else if(this.translator.isTranslationByFloatingEnabled()) {
  108.                     // if no panel
  109.                     if(!this.floatingPanel) return true;
  110.                     
  111.                     // if panel itself
  112.                     if(this.floatingPanel && $(e.target).closest('.translator-floating-panel').length > 0) return true;
  113.                     
  114.                     this.floatingPanel.setPosition(e.clientX, e.clientY);
  115.                     this.floatingPanel.show();
  116.                 }
  117.             }.bind(this));
  118.         },
  119.         
  120.         removeMouseListeners: function()
  121.         {
  122.             $(this.doc).unbind('mouseup.translator');
  123.         },
  124.         
  125.         translateSelection: function()
  126.         {
  127.             var selectedText = this.getSelectedText();
  128.             
  129.             if(selectedText.length == 0) return;
  130.             
  131.             this.showLoadingMessage();
  132.             
  133.             this.translator.translate(selectedText, this.translatorCallback.bind(this));
  134.         },
  135.         
  136.         translatorCallback: function(status, translation, notice)
  137.         {
  138.             switch(status) {
  139.                 case namespace.Translator.STATUS_NOT_DETECTED:
  140.                     this.showErrorMessage(this.properties.getString('languageNotDetected'));
  141.                     break;
  142.                 
  143.                 case namespace.Translator.STATUS_TRANSLATED:
  144.                     this.showMessage(translation, notice);
  145.                     break;
  146.                 
  147.                 case namespace.Translator.STATUS_NOT_TRANSLATED:
  148.                     this.showErrorMessage(this.properties.getString('unableToTranslate'));
  149.                     break;
  150.             }
  151.         },
  152.         
  153.         showMessage: function(text, notice)
  154.         {
  155.             this.popup.setMessage(text);
  156.             this.popup.setNotice(notice);
  157.             this.popup.showMessage();
  158.         },
  159.         
  160.         showErrorMessage: function(text)
  161.         {
  162.             this.popup.setMessage('');
  163.             this.popup.setNotice(text);
  164.             this.popup.showError();
  165.         },
  166.         
  167.         showLoadingMessage: function()
  168.         {
  169.             this.popup.setMessage('loading...');
  170.             this.popup.setNotice('');
  171.             this.popup.showMessage();
  172.         },
  173.         
  174.         getSelectedText: function()
  175.         {
  176.             // add empty string to make it's just a plain string
  177.             // otherwise there are some problems with security veto
  178.             return this.window.getSelection() + '';
  179.         },
  180.         
  181.         isTextSelected: function()
  182.         {
  183.             return (this.getSelectedText().length > 0);
  184.         },
  185.         
  186.         
  187.         /* event handlers */
  188.         
  189.         translateSelectionHandler: function(e)
  190.         {
  191.             // reset popup position (bottom corner)
  192.             this.popup.resetPosition();
  193.             
  194.             // hide floating pane (if it was shown)
  195.             this.floatingPanel.hide();
  196.             
  197.             // translate selected text
  198.             this.translateSelection();
  199.         },
  200.         
  201.         translatorEnabledHandler: function(e)
  202.         {
  203.             this.addTranslatorListeners();
  204.         },
  205.         
  206.         translatorDisabledHandler: function(e)
  207.         {
  208.             this.removeTranslatorListeners();
  209.             
  210.             // hide translation popup
  211.             this.popup.hide();
  212.         },
  213.         
  214.         translateSelectionEnabledHandler: function(e)
  215.         {
  216.             this.addMouseListeners();
  217.         },
  218.         
  219.         translateSelectionDisabledHandler: function(e)
  220.         {
  221.             if(!this.translator.isTranslationByFloatingEnabled()) {
  222.                 this.removeMouseListeners();
  223.             }
  224.             
  225.             // hide translation popup
  226.             this.popup.hide();
  227.         },
  228.         
  229.         translateFloatingHandler: function(e, state)
  230.         {
  231.             // enabled
  232.             if(state) {
  233.                 this.addMouseListeners();
  234.             }
  235.             // disabled
  236.             else {
  237.                 if(this.translator.isTranslationBySelectionEnabled()) {
  238.                     this.removeMouseListeners();
  239.                 }
  240.                 
  241.                 // hide floating panel popup
  242.                 this.floatingPanel.hide();
  243.             }
  244.         },
  245.         
  246.         pageUnloadHandler: function(e)
  247.         {
  248.             this.removeListeners();
  249.         }
  250.     };
  251. })(com.igorgladkov.translator, translatorJQuery);